<template>
  <van-button
    v-if="value"
    round
    size="small"
    @click="onFollow"
    :loading="loading"
    >已关注</van-button
  >
  <van-button
    v-else
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFollow"
    :loading="loading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";
export default {
  name: "FollowUser",
  props: {
    // 是否关注用户
    value: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },

  data() {
    return {
      loading: false
    };
  },
  computed: {},

  created() {},

  mounted() {},

  watch: {},

  methods: {
    async onFollow() {
      //  如果没有登录 就不允许登录
      if (!this.$store.state.user) return this.$toast("请登录");
      //  开启加载 loading 状态
      this.loading = true;
      try {
        // 如果关注，则取消关注
        // const authorId = this.article.aut_id;
        if (this.value) {
          await deleteFollow(this.userId);
        } else {
          // 否则添加关注
          await addFollow(this.userId);
        }
        // 更新视图
        this.$emit("input", !this.value);
      } catch (err) {
        if (err && err.response.status === 400) {
          this.$toast("你不能关注自己");
        } else {
          this.$toast.fail("操作失败");
        }
      }
      this.loading = false;
    }
  },
  components: {}
};
</script>

<style scoped lang="less"></style>
